package com.mouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mouse.entity.*;
import com.mouse.entity.vo.GeneCategoryVO;
import com.mouse.mapper.*;
import com.mouse.service.GeneCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mouse.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author godson
 * @since 2022-08-05
 */
@Service
public class GeneCategoryServiceImpl extends ServiceImpl<GeneCategoryMapper, GeneCategory> implements GeneCategoryService {

    @Autowired
    GeneCategoryMapper geneCategoryMapper;

    @Autowired
    private DiseaseMapper diseaseMapper;

    @Autowired
    private DoubleAllelesMapper doubleAllelesMapper;

    @Autowired
    private GeneCompositionMapper geneCompositionMapper;

    @Autowired
    private PhenotypeMapper phenotypeMapper;

    @Autowired
    private ReferenceMapper referenceMapper;

    @Autowired
    private GeneCompositionDoubleAllelesMapper geneCompositionDoubleAllelesMapper;

    @Autowired
    private MouseMapper mouseMapper;

    @Autowired
    private GeneReferenceMapper geneReferenceMapper;

    @Autowired
    private GeneCompositionReferenceMapper geneCompositionReferenceMapper;

    @Autowired
    private GenecategoryGenecompositionMapper genecategoryGenecompositionMapper;

    @Autowired
    private GeneAlleleMapper geneAlleleMapper;
    @Autowired
    private GeneHomologyMapper geneHomologyMapper;

    @Autowired
    private DoubleAllelesGeneAlleleMapper doubleAllelesGeneAlleleMapper;


    /***
     * 查询：根据一个字段信息查询表格信息，支持字段值类型为ID和String
     * @param page 分页设置
     * @param key 字段名
     * @param value 字段值
     * @return 查询结果
     */
    @Override
    public IPage<GeneCategory> getInfoByMap(IPage<GeneCategory> page, String key, String value) {
        long id;
        QueryWrapper<GeneCategory> wrapper = new QueryWrapper<>();
        if (StringUtil.isInteger(value)) {
            id = Long.parseLong(value);
            wrapper.eq(key, id);
        } else {
            wrapper.eq(key, value);
        }
        return baseMapper.selectPage(page, wrapper);
    }
    @Override
    public GeneCategoryVO getGeneCategoryVO(GeneCategory geneCategory){
        /**
         * key:gc.gid
         */
         Map<Long, List<GeneComposition>> geneCompositionMap=new HashMap<>();


        /**
         *  key:gcom.gcid
         */

         Map<Long, List<DoubleAlleles>> doubleAllelesMap=new HashMap<>();


        /**
         * key:gcom.gcid
         */
         Map<Long, List<Reference>> geneCompositionReferenceMap=new HashMap<>();

        /**
         * key:gc.gcid
         */
        Map<Long, List<Reference>> geneCategoryReferenceMap=new HashMap<>();


        /**
         * key:gcom.gcid
         */
         Map<Long, List<Phenotype>> phenotypeMap=new HashMap<>();

        /**
         * key:da.did
         */
         Map<Long, List<GeneAllele>> geneAlleleMap=new HashMap<>();

        /**
         * key:gc.gid
         */
         Map<Long,List<Disease>> diseaseMap=new HashMap<>();
        Long id = geneCategory.getGid();

        //GeneComposition相关
        List<GenecategoryGenecomposition> genecategoryGenecompositionByGid = genecategoryGenecompositionMapper.getGenecategoryGenecompositionByGid(id);
        List<GeneComposition> geneCompositionByGid = geneCompositionMapper.getGeneCompositionByGid(id);
        for(GenecategoryGenecomposition relation: genecategoryGenecompositionByGid){
            geneCompositionMap.computeIfAbsent(relation.getGid(), k -> new ArrayList<>());
            for (GeneComposition entity:geneCompositionByGid){
                if(entity.getGcid().equals(relation.getGcid())) {
                    geneCompositionMap.get(relation.getGid()).add(entity);
                }
            }
        }

        List<GeneCompositionDoubleAlleles> geneCompositionDoubleAllelesByGid = geneCompositionDoubleAllelesMapper.getGeneCompositionDoubleAllelesByGid(id);
        List<DoubleAlleles> doubleAllelesByGid = doubleAllelesMapper.getDoubleAllelesByGid(id);
        for(GeneCompositionDoubleAlleles relation: geneCompositionDoubleAllelesByGid){
            doubleAllelesMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (DoubleAlleles entity:doubleAllelesByGid){
                if(entity.getDid().equals(relation.getDid())) {
                    doubleAllelesMap.get(relation.getGcid()).add(entity);
                }
            }
        }


        List<GeneCompositionReference> geneCompositionReferenceByGid = geneCompositionReferenceMapper.getGeneCompositionReferenceByGid(id);
        List<Reference> referenceAboutGeneCompositionByGid = referenceMapper.getReferenceAboutGeneCompositionByGid(id);
        for(GeneCompositionReference relation: geneCompositionReferenceByGid){
            geneCompositionReferenceMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (Reference entity:referenceAboutGeneCompositionByGid){
                if(entity.getRid().equals(relation.getRid())) {
                    geneCompositionReferenceMap.get(relation.getGcid()).add(entity);
                }
            }
        }

        List<Mouse> mouseByGid = mouseMapper.getMouseByGid(id);
        List<Phenotype> phenotypeByGid = phenotypeMapper.getPhenotypeByGid(id);
        for(Mouse relation: mouseByGid){
            phenotypeMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (Phenotype entity:phenotypeByGid){
                if(entity.getPid().equals(relation.getPid())) {
                    phenotypeMap.get(relation.getGcid()).add(entity);
                }
            }
        }

        List<DoubleAllelesGeneAllele> doubleAllelesGeneAlleleByGid = doubleAllelesGeneAlleleMapper.getDoubleAllelesGeneAlleleByGid(id);
        List<GeneAllele> geneAllelesByGid = geneAlleleMapper.getGeneAllelesByGid(id);
        for(DoubleAllelesGeneAllele relation: doubleAllelesGeneAlleleByGid){
            geneAlleleMap.computeIfAbsent(relation.getDid(), k -> new ArrayList<>());
            for (GeneAllele entity:geneAllelesByGid){
                if(entity.getAid().equals(relation.getAid())) {
                    geneAlleleMap.get(relation.getDid()).add(entity);
                }
            }
        }

        List<GeneReference> geneReferenceByGid = geneReferenceMapper.getGeneReferenceByGid(id);
        List<Reference> referenceAboutGeneCategoryByGid = referenceMapper.getReferenceAboutGeneCategoryByGid(id);
        for(GeneReference relation: geneReferenceByGid){
            geneCategoryReferenceMap.computeIfAbsent(relation.getGid(), k -> new ArrayList<>());
            for (Reference entity:referenceAboutGeneCategoryByGid){
                if(entity.getRid().equals(relation.getRid())) {
                    geneCategoryReferenceMap.get(relation.getGid()).add(entity);
                }
            }
        }

        List<GeneHomology> geneHomologyByGid = geneHomologyMapper.getGeneHomologyByGid(id);
        List<Disease> diseaseByGid = diseaseMapper.getDiseaseByGid(id);
        for(GeneHomology relation: geneHomologyByGid){
            diseaseMap.computeIfAbsent(relation.getGid(), k -> new ArrayList<>());
            for (Disease entity:diseaseByGid){
                if(entity.getSid().equals(relation.getSid())) {
                    diseaseMap.get(relation.getGid()).add(entity);
                }
            }
        }

        GeneCategoryVO geneCategoryVO = new GeneCategoryVO();
        geneCategoryVO.setGeneCategory(geneCategory);
        geneCategoryVO.setGeneCompositionMap(geneCompositionMap);
        geneCategoryVO.setDoubleAllelesMap(doubleAllelesMap);
        geneCategoryVO.setGeneCompositionReferenceMap(geneCompositionReferenceMap);
        geneCategoryVO.setPhenotypeMap(phenotypeMap);
        geneCategoryVO.setGeneAlleleMap(geneAlleleMap);
        geneCategoryVO.setGeneCategoryReferenceMap(geneCategoryReferenceMap);
        geneCategoryVO.setDiseaseMap(diseaseMap);


    return geneCategoryVO;
    }




//    @Autowired
//    private DiseaseMapper diseaseMapper;
//
//    @Autowired
//    private DoubleAllelesMapper doubleAllelesMapper;
//
//    @Autowired
//    private GeneAlleleMapper geneAlleleMapper;
//
//    @Autowired
//    private GeneCompositionMapper geneCompositionMapper;
//
//    @Autowired
//    private PhenotypeMapper phenotypeMapper;
//
//    @Autowired
//    private ReferenceMapper referenceMapper;


//    @Override
//    public Page<GeneCategoryDerivation> getDerivativeResult(Page<GeneCategoryDerivation> page, GeneCategory geneCategory) {
//        Long gid = geneCategory.getGid();
//    }
//
//    @Override
//    public Page<GeneCategoryDerivation> getDerivativeResult(Page<GeneCategoryDerivation> page, Long gid) {
//        return new GeneCategoryDerivation(geneCompositionMapper.getGeneCompositionByGid(gid),)
//    }

//    @Override
//    public Page<GeneCategory> getGeneCategoryByGeneID(Page<GeneCategory> page, Long geneID) {
//        return geneCategoryMapper.getGeneCategoryByGeneID(page, geneID);
//    }
//
//    @Override
//    public Page<GeneCategory> getGeneCategoryByGeneName(Page<GeneCategory> page, String name) {
//        return geneCategoryMapper.getGeneCategoryByGeneName(page, name);
//    }
//
//    @Override
//    public Page<GeneCategory> getGeneCategoryByGeneAlias(Page<GeneCategory> page, String alias) {
//        return geneCategoryMapper.getGeneCategoryByGeneAlias(page, alias);
//    }
//
//    @Override
//    public Page<GeneCategory> getGeneCategoryByGeneHomology(Page<GeneCategory> page, String homology) {
//        return geneCategoryMapper.getGeneCategoryByGeneHomology(page, homology);
//    }


}